// pages/reservation/clubCard/clubCard.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classType:null,
    checkedValue:null, //卡的ID
    price:0,           //选中规格的价格
    morePrice:0,
    cardNum:0,      //次数卡次数
    checkedAgreement: false,//是否勾选同意
    cardType:null,     //卡的类型 1为次数卡，2为充值卡
    salesmanText: '',
    salesmanValue: [],
    salesmanTitle: '',
    salesmans: [], //销售人员
    clubCardList: [] //卡的列表
  },

  // 封装获取动态数据的函数
  getDataCard(options) {
    let isUrl = null
    if (options.id == 1) {
      isUrl = 'http://localhost:8080/keep/cardNum/list'
    }else{
      isUrl = 'http://localhost:8080/keep/card/list'
    }
    wx.request({
      url: isUrl,
      data: {
        del: 0,
      },
      method: 'GET',
      header: {
        'Authorization': getApp().globalData.authorization
      },
      success: resp => {
        this.setData({
          clubCardList: this.data.clubCardList.concat(resp.data.rows),
        })
        console.log(resp.data)
      },
    })
  },
// 立即购买扣去余额
  setBalance() {
    if(this.data.price > 0 && this.data.checkedAgreement){
      wx.request({
        url: 'http://localhost:8080/keep/balance',
        data: {
          userId:getApp().globalData.loginfo.userId,
          residuePrice:-this.data.price,
          cardType:this.data.cardType,
          cardNum:this.data.cardNum,
          morePrice:this.data.morePrice
        },
        method: 'PUT',
        header: {
          'Authorization': getApp().globalData.authorization
        },
        success: resp => {
          if (resp.data.code == 200) {
            Toast.success('购买成功');
            console.log('---------购买成功-----------')
          }else{
            Toast.fail('余额不足');
            console.log('---------购买失败-----------')
          }
        },
      })
      
    }else{
      if(this.data.price > 0 || this.data.checkedAgreement){
        if(this.data.checkedAgreement){
          Toast('请选择规格');
        }else{
        Toast('请阅读并勾选会员服务协议');
        }
      }else{
        Toast('请选择规格');
      }
    }
  },


  // 选择器
  onColumnChange(e) {
    console.log('picker pick:', e);
  },

  onPickerChange(e) {
    const {
      key
    } = e.currentTarget.dataset;
    const {
      value
    } = e.detail;

    console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: value.join(' '),
    });
  },

  onPickerCancel(e) {
    const {
      key
    } = e.currentTarget.dataset;
    console.log(e, '取消');
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },

  onTitlePicker() {
    this.setData({
      salesmanVisible: true,
      salesmanTitle: '选择销售人员'
    });
  },

  // 单选框
  handleChange(e) {
    this.setData({
      checkedValue: e.detail.value,
    });
    this.data.clubCardList.forEach(element => {
      let isCardNum =null
      if (this.data.cardType == 1) {
        isCardNum = element.number
      }
      if (element.id == e.detail.value) {
        this.setData({
          price:element.nowPrice,
          morePrice:element.beforePrice,
          cardNum:isCardNum
        })
        return;
      }else if (this.data.checkedValue == null) {
        this.setData({
          price:0,
          cardNum:0,
          morePrice:0
        })
      }
      
      console.log(this.data.price)
    });

    console.log(this.data.checkedValue)
  },
  // 勾选同意
  onChange(event) {
    this.setData({
      checkedAgreement: event.detail,
    });
    console.log(event.detail)
  },
  // 返回上一页
  onNavigate() {
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDataCard(options);
    this.setData({
      cardType:options.id,
      classType:options.id == 1 ?'containerCard_1' : 'containerCard_2'
    })
    console.log(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})